package com.yimin.controller;


import com.yimin.GenCode.InterfaceS.GenMybatisXmlAndServer;
import com.yimin.GenCode.InterfaceS.GenWebApiServer;
import com.yimin.GenCode.MainGenCode;
import com.yimin.Utils.FileDownUtil;
import com.yimin.Utils.MyHttpRequest;
import com.yimin.databaseS.DatabaseEntity;
import com.yimin.databaseS.DatabaseTableFieldEntity;
import com.yimin.entity.ComSEntity;
import com.yimin.jpa.databaseS.ComDatabaseFieldServerApi;
import com.yimin.jpa.databaseS.ComDatabaseServerApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

import static com.yimin.GenCode.imp.GenMybatisXmlAndServerImp.writeFileByString;
import static com.yimin.GenCode.imp.GetApPathByTypeAndTableNameImp.humpTransition;

@RestController
@RequestMapping("gen")
public class MainGenCodeController {



    @Resource
    MainGenCode mainGenCode;


    @Value("${gen.code.codePath}")
    String codePath;

    @Value("${gen.code.codeResourcesPath}")
    String codeResourcesPath;

    @Value("${gen.code.codeTePath}")
    String codeTePath;


    @Resource
    ComDatabaseServerApi comDatabaseServerApi;

    @Resource
    ComDatabaseFieldServerApi comDatabaseFieldServerApi;

    @Resource
    GenMybatisXmlAndServer genMybatisXmlAndServer;

    @Resource
    GenWebApiServer genWebApiServer;


    /**
     * 这里表示当前开始执行某个项目的自动代码生成
     * @return
     */
    @GetMapping("code")
    MyHttpRequest getAll(@RequestParam String projectId){
        //每次需要删除旧的文件夹
        File GenCodeFile=new File(codePath);
        if(GenCodeFile.exists()){

            GenCodeFile.delete();
        }

        File zipZFile=new File("GenCode.zip");
        if(zipZFile.exists()){

            zipZFile.delete();
        }

     List<DatabaseEntity> tableS=comDatabaseServerApi.findAllByProjectId(projectId);


        tableS.forEach(item->{

            List<DatabaseTableFieldEntity> fieldEntityList=comDatabaseFieldServerApi.findAllByTableId(item.getId());

            try {

                writeFileByString(codeResourcesPath+"/mapper/"+humpTransition(item.getName())+".xml",genMybatisXmlAndServer.genMybatisXml(fieldEntityList,item.getName()));
                writeFileByString(codePath+"/mapper/"+"MybatisServer"+humpTransition(item.getName())+".java",genMybatisXmlAndServer.genMybatisServer(fieldEntityList,item.getName()));
                writeFileByString(codePath+"/controller/"+humpTransition(item.getName())+"ApiController.java",genWebApiServer.genWebApi(item.getName()));

                File zipFile=new File("GenCode.zip");
                if(!zipFile.exists()){
                    zipFile.createNewFile();
                }
                OutputStream   outputStream= new FileOutputStream(zipFile);
                FileDownUtil.toZip(codeTePath,outputStream,true);

            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        });


        return new MyHttpRequest("计划创建成功",200,null);

    }



    @GetMapping("down")
    void down(HttpServletResponse response) throws IOException {
        //每次需要删除旧的文件夹
        InputStream  inputStream=new FileInputStream("GenCode.zip");
        FileDownUtil.downloadFile(response,"GenCode.zip",inputStream);


    }


}
